package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.util.JwtUtil;
import com.atguigu.lease.common.util.VerifyCodeUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.mapper.UserInfoMapper;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.SmsService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.atguigu.lease.web.app.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    SmsService smsService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserInfoMapper userInfoMapper;


    @Override
    public void getCode(String phone) {
        if (!StringUtils.hasText(phone)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }

        if(stringRedisTemplate.hasKey(RedisConstant.APP_LOGIN_PREFIX + phone)){
            Long expire = stringRedisTemplate.getExpire(RedisConstant.APP_LOGIN_PREFIX + phone);
            if(RedisConstant.APP_LOGIN_CODE_TTL_SEC-expire<RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC){
                throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
            }
        }

        String key = RedisConstant.APP_LOGIN_PREFIX + phone;
        String verifyCode =VerifyCodeUtil.getVerifyCode(6);
        smsService.sendCode(phone, verifyCode);
        stringRedisTemplate.opsForValue().set(key, verifyCode,RedisConstant.APP_LOGIN_CODE_TTL_SEC, TimeUnit.SECONDS);

    }

    @Override
    public String login(LoginVo loginVo) {
        String phone = loginVo.getPhone();
        String code = loginVo.getCode();

//  TODO      if(!StringUtils.hasText(phone)){
//            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
//        }
//        if (!StringUtils.hasText(code)) {
//            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
//        }

//        String key = RedisConstant.APP_LOGIN_PREFIX + phone;
//        String value = stringRedisTemplate.opsForValue().get(key);
//        if (value == null){
//            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
//        }
//        if(!value.equals(code)){
//            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
//        }

        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getPhone, phone));
//        if(userInfo==null){
//            userInfo = new UserInfo();
//            userInfo.setPhone(phone);
//            userInfo.setNickname("用户-" + phone.substring(7));
//            userInfo.setStatus(BaseStatus.ENABLE);
//            userInfoMapper.insert(userInfo);
//        }

//        if (userInfo.getStatus().equals(BaseStatus.DISABLE)) {
//            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
//        }

        return JwtUtil.createToken(userInfo.getId(), userInfo.getPhone());

    }

    @Override
    public UserInfoVo getByUserId(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return new UserInfoVo(userInfo.getNickname(),userInfo.getAvatarUrl());
    }
}
